package com.rjh.concurrent.create;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

@Slf4j(topic = "c.CountTestDemo")
public class CountTestDemo {

    private static int count = 0;

    static final Object room = new Object();

    public static void main(String[] args) throws InterruptedException {

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int k = 0; k <= 1000; k++) {
                    synchronized (room) {
                        count++;
                    }
                }
            }
        });

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int k = 0; k <= 1000; k++) {
                    synchronized (room) {
                        count--;
                    }
                }
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();
        TimeUnit.SECONDS.sleep(2);
        log.info("count:{}", count);
    }
}
